<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends Users_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	}

	function index()
	{
		$uilang = $this->session->userdata('uilang');
		
		// get the current user and its profile
		$u = new User();
		$u->where('id', $this->tank_auth->get_user_id())->get();
		
		$u->user_profile->limit(1)->get();
		$u->user_profile->country->limit(1)->get();
		
		// get a list with all the countries in the ddbb
		$c = new Country();
		$c->order_by($uilang, "ASC");
		$c->get();
		
		$top_countries = array();
		$regular_countries = array();
		
		foreach($c as $country) {
			if($country->id == 2 || $country->id==3 || $country->id==4) {
				$top_countries[$country->id] = $country->$uilang;
			}
			else {
				$regular_countries[$country->id] = $country->$uilang;
			}
		}
		$countries = $top_countries + array(""=>"----------------------------") + $regular_countries;

		$data = array(
			'user'			=> $u,
			'countries'		=> $countries,
			'avatar_list'	=> array("" => "") + $this->imageene->get_avatars()
		);
		
 		$this->imageene->user('profile/profile', $data);
	}
	
	function show_public($user) {
		echo "$user's public profile";
	}
	
	function update()
	{
		$this->form_validation->set_rules('first_name', 'First name', 'trim|xss_clean|alpha|max_length[45]');
		$this->form_validation->set_rules('middle_name', 'Middle name', 'trim|xss_clean|alpha|max_length[1]');
		$this->form_validation->set_rules('last_name', 'Last name', 'trim|xss_clean|alpha|max_length[45]');
		$this->form_validation->set_rules('alias', 'Alias', 'trim|xss_clean|max_length[45]');
		$this->form_validation->set_rules('location', 'Location', 'trim|xss_clean|max_length[45]');
		$this->form_validation->set_rules('country', 'Country', 'integer');
		$this->form_validation->set_rules('web', 'Website', 'trim|xss_clean|max_length[100]');
		$this->form_validation->set_rules('avatar', 'Avatar', 'trim|xss_clean|max_length[100]');
		$this->form_validation->set_rules('user_gravatar', 'Use Gravatar', 'integer');
		$this->form_validation->set_error_delimiters('<span class="error">','</span>');
		
		if ($this->form_validation->run()) {
			$data = $this->input->post();
			
			$u = new User();
			$u->where('id', $this->tank_auth->get_user_id())->get();
			$p = $u->profile->limit(1)->get();
			
			$p->first_name		= $data["first_name"];
			$p->middle_name		= $data["middle_name"];
			$p->last_name		= $data["last_name"];
			$p->alias			= $data["alias"];
			$p->location		= $data["location"];
			$p->country			= intval($data["country"]);
			$p->web				= $data["web"];
			$p->avatar			= $data["avatar"];
			$p->gravatar = (isset($data["use_gravatar"])) ? $this->gravatar->getEmailHash($u->email) : NULL ;
			
			$p->save();
		}
		redirect('/user/profile');
	}
}
	
/* End of file profiles.php */
/* Location: ./application/controllers/profiles.php */